package com.wangkang.ucenter.controller;
import com.wangkang.commonutils.JwtUtils;
import com.wangkang.commonutils.R;
import com.wangkang.commonutils.model.vo.MemberOrderVo;
import com.wangkang.ucenter.entity.Member;
import com.wangkang.ucenter.model.form.MemberForm;
import com.wangkang.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


@Api(tags = "会员管理")
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }


    /**
     * 用户登录
     */

    @PostMapping("/login")
    @ApiOperation("会员登录")
    public R login(@RequestBody Member member){
        //member 对象封装手机号和密码

        //返回token值,使用jwt生成
        String token = memberService.login(member);

        return R.ok().data("token",token);
    }

    /**
     * 用户注册
     *
     */

    @PostMapping("/register")
    @ApiOperation("会员注册")
    public R register(@RequestBody MemberForm memberForm){
        memberService.register(memberForm);
        return R.ok();
    }

    /**
     **根据token获取会员信息，用于前端页面展示
     **传入参数HttpServletRequest对象
     */
    @ApiOperation("获取会员信息")
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据Request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        //查询数据库根据用户id获取用户信息
        Member member = memberService.getById(memberId);
        return R.ok().data("member",member);
    }

    @ApiOperation("根据会员id获取用户信息")
    @GetMapping("/getMemberInfoOrder/{id}")
    public MemberOrderVo getMemberInfoOrderById(@PathVariable String id){
        Member member = memberService.getById(id);
        MemberOrderVo memberOrderVo = new MemberOrderVo();
        BeanUtils.copyProperties(member,memberOrderVo);
        return memberOrderVo;
    }

    @GetMapping("/countRegister/{day}")
    public Integer countRegister(@PathVariable String day){
        Integer count =  memberService.countRegisterDay(day);
        return count;
    }

}